package com.rpframework.website.device.act.api;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.rpframework.core.BaseAct;
import com.rpframework.core.utils.TagUtils;
import com.rpframework.utils.Pager;
import com.rpframework.website.device.domain.Bank;
import com.rpframework.website.device.domain.BankUser;
import com.rpframework.website.device.domain.User;
import com.rpframework.website.device.exception.APICodeException;
import com.rpframework.website.device.service.BankService;
import com.rpframework.website.device.service.BankUserService;
@Controller
@RequestMapping("api/bankuser")
public class ApiBankUserAct extends BaseAct{
	@Resource BankUserService service;
	@Resource BankService bankService;
	
	@RequestMapping(value="/add",produces = "application/json; charset=utf-8")
	public @ResponseBody String add(
			@RequestParam(value = "bankId") Integer bankId,
			@RequestParam(value = "name") String name,
			@RequestParam(value = "cardNo") String cardNo,
			@RequestParam(value = "tel") String tel,
			@RequestParam(value = "address") String address,
			@RequestParam(value = "callback",required = false ) String callback,
			HttpSession session){
		User sessionuser=getSessionUser(session);
		if(sessionuser==null){
			throw new APICodeException(-4, "你还没登陆!");
		}
		JsonObject json = new JsonObject();
		BankUser t = new BankUser();
		t.setAddress(address);
		t.setBankId(bankId);
		t.setCardNo(cardNo);
		t.setCreateTime(System.currentTimeMillis()/1000);
		t.setTel(tel);
		t.setName(name);
		t.setUserId(sessionuser.getId());
		boolean flag = service.insert(t);
		if(flag){
			json.addProperty("code", 200);
			json.addProperty("msg", "添加成功");
		}else{
			json.addProperty("code", 500);
			json.addProperty("msg", "添加失败");
		}
		if(StringUtils.isNotBlank(callback))return (callback + "(" + json + ")");
		return ""+json ;
	}	
	
	@RequestMapping(value="/list",produces = "application/json; charset=utf-8")
	public @ResponseBody String list(@RequestParam(value="pager",required = false ) Pager<BankUser> pager,
			@RequestParam(value = "callback",required = false ) String callback,
			HttpSession session
			){
		if(pager==null){
			pager=new Pager<BankUser>();
		}
		User sessionUser = (User) session.getAttribute(SESSION_USER_KEY);
		pager.getSearchMap().put("userId", sessionUser.getId().toString());
		pager = service.getPager(pager);
		JsonObject json=new JsonObject();
		json.addProperty("totalPages", pager.getTotalPages());
		json.addProperty("currentPage", pager.getCurrentPage());
		json.addProperty("totalCount", pager.getTotalCount());
		List<BankUser> list=pager.getItemList();
		if(list!=null && list.size()>0){
		JsonArray array=new JsonArray();
			for(BankUser t : list){
				JsonObject obj = new JsonObject();
					obj.addProperty("id", t.getId());	
					Bank bank = bankService.select(t.getBankId());
					obj.addProperty("image",TagUtils.getFileFullPath(bank.getImage()));
					obj.addProperty("name", bank.getName());
					obj.addProperty("cardNo", t.getCardNo());
					obj.addProperty("type",bank.getType());
					array.add(obj);
			}
			json.add("arrays", array);
		}
		if(StringUtils.isNotBlank(callback))return (callback + "(" + json + ")");
		return ""+json ;
	}	
}
